package XMLManager;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringWriter;

import org.xmlpull.v1.XmlSerializer;

import android.content.Context;
import android.os.Environment;
import android.util.Log;
import android.util.Xml;


public class yXMLWriter {
	private FileOutputStream fileos; 
	private String filename;
	private String enc;
	private String root;
	private File file;
	private XmlSerializer xs = Xml.newSerializer();
	private StringWriter writer = new StringWriter();
	
	
	public yXMLWriter(String root, String filename, String enc){
		try {
			this.root = root;
			file = new File(Environment.getExternalStorageDirectory() + "/" + 
			filename);
			fileos = new FileOutputStream(file, false);
			Log.d("XML OPEN:", ""+file.exists()+fileos.toString());
			
			xs.setOutput(fileos, enc);
			xs.startDocument(null, true);
			xs.startTag(null, root);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void close(){
		//Log.d("XML CLOSE:", ""+file.exists()+fileos.toString());
		//String dataWrite = writer.toString();
		try {
			xs.endTag(null, root);
			xs.endDocument();
			xs.flush();
			
			fileos.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void newPattern(String tname, String tpatternUnit, int tprogramType, String texecution){
		try {
			xs.startTag(null, XML_P.PATTERN_SET).attribute(null, XML_P.name, tname);
			{
				xs.startTag(null, XML_P.PATTERN_UNIT);
				{
					xs.text(tpatternUnit);
				}
				xs.endTag(null, XML_P.PATTERN_UNIT);
				
				xs.startTag(null, XML_P.EXECUTION_INFO).attribute(null, XML_P.type, Integer.toString(tprogramType));
				{
					xs.text(texecution);
				}
				xs.endTag(null, XML_P.EXECUTION_INFO);
			}
			xs.endTag(null, XML_P.PATTERN_SET);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void newSettings(int [] tpadColor, int tsensitivity, int ttimelimit, boolean lock, boolean onBoot){
		try {
			xs.startTag(null, XML_S.PAD_COLOR);
			xs.attribute(null, XML_S.r, Integer.toString(tpadColor[0]));
			xs.attribute(null, XML_S.g, Integer.toString(tpadColor[1]));
			xs.attribute(null, XML_S.b, Integer.toString(tpadColor[2]));
			{
			}
			xs.endTag(null, XML_S.PAD_COLOR);
			
			xs.startTag(null, XML_S.SENSITIVITY);
			{
				xs.text(Integer.toString(tsensitivity));
			}
			xs.endTag(null, XML_S.SENSITIVITY);
			
			xs.startTag(null, XML_S.TIME_LIMIT);
			{
				xs.text(Integer.toString(ttimelimit));
			}
			xs.endTag(null, XML_S.TIME_LIMIT);
			
			xs.startTag(null, XML_S.LOCK);
			xs.attribute(null, XML_S.onBoot, Boolean.toString(onBoot));
			{
				xs.text(Boolean.toString(lock));
			}
			xs.endTag(null, XML_S.LOCK);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
